{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f1bdc814",
   "metadata": {},
   "outputs": [],
   "source": [
    "'''\n",
    "1. 基本配置\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "885fd05e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 源地址：https://blog.csdn.net/weixin_43593330/article/details/108347949\n",
    "'''\n",
    "导入包和版本查询\n",
    "'''\n",
    "import torch\n",
    "import torch.nn as nn\n",
    "import torchvision\n",
    "\n",
    "\n",
    "print(torch.__version__)\n",
    "print(torch.version.cuda)\n",
    "print(torch.backends.cudnn.version())\n",
    "print(torch.cuda.get_device_name(0))#获取显卡"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "d32de633",
   "metadata": {},
   "outputs": [],
   "source": [
    "'''\n",
    "可复现性\n",
    "在硬件设备（CPU、GPU）不同时，完全的可复现性无法保证，即使随机种子相同。\n",
    "但是，在同一个设备上，应该保证可复现性。具体做法是，在程序开始的时候固定\n",
    "torch的随机种子，同时也把numpy的随机种子固定。\n",
    "'''\n",
    "\n",
    "'''\n",
    "导入包和版本查询\n",
    "'''\n",
    "import torch\n",
    "import torch.nn as nn\n",
    "import torchvision\n",
    "import numpy as np\n",
    "\n",
    "np.random.seed(0)\n",
    "torch.manual_seed(0)\n",
    "torch.cuda.manual_seed_all(0)\n",
    "\n",
    "torch.backends.cudnn.deterministic = True\n",
    "torch.backends.cudnn.benchmark = False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "a1bd1166",
   "metadata": {},
   "outputs": [],
   "source": [
    "'''\n",
    "显卡设置\n",
    "如果只需要一张显卡\n",
    "'''\n",
    "# Device configuration\n",
    "device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "abdcbe44",
   "metadata": {},
   "outputs": [],
   "source": [
    "'''\n",
    "如果需要指定多张显卡，比如0，1号显卡。\n",
    "也可以在命令行运行代码时设置显卡：\n",
    "\n",
    "'''\n",
    "import os\n",
    "os.environ['CUDA_VISIBLE_DEVICES'] = '0,1'\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "39419531",
   "metadata": {},
   "outputs": [],
   "source": [
    "'''\n",
    "也可以在命令行运行代码时设置显卡：\n",
    "'''\n",
    "CUDA_VISIBLE_DEVICES=0,1 python train.py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ddf6150e",
   "metadata": {},
   "outputs": [],
   "source": [
    "'''\n",
    "清除显存\n",
    "'''\n",
    "torch.cuda.empty_cache()\n",
    "# 只有执行完这句，显存才会在Nvidia-smi中释放"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d9440901",
   "metadata": {},
   "outputs": [],
   "source": [
    "'''\n",
    "也可以使用在命令行重置GPU的指令\n",
    "'''\n",
    "nvidia-smi --gpu-reset -i [gpu_id]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c8909ba5",
   "metadata": {},
   "outputs": [],
   "source": [
    "'''\n",
    "2. 张量(Tensor)处理\n",
    "张量的数据类型\n",
    "PyTorch有9种CPU张量类型和9种GPU张量类型。\n",
    "''' "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "a8ffb366",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "torch.FloatTensor\n",
      "torch.Size([3, 4, 5])\n",
      "3\n",
      "tensor([[[-1.1256e+00, -3.1700e-01, -1.0925e+00, -8.5194e-02, -9.3348e-02],\n",
      "         [ 6.8705e-01, -8.3832e-01,  8.9182e-04, -7.5043e-01,  1.8541e-01],\n",
      "         [ 6.2114e-01,  6.3818e-01, -2.4600e-01,  2.3025e+00, -1.8817e+00],\n",
      "         [-4.9727e-02,  1.9415e+00,  7.9150e-01, -2.0252e-02, -4.3717e-01]],\n",
      "\n",
      "        [[ 1.6459e+00, -1.3602e+00,  3.4457e-01,  5.1987e-01, -3.6562e-01],\n",
      "         [-1.3024e+00,  9.9403e-02,  4.4182e-01,  2.4693e-01,  7.6887e-02],\n",
      "         [ 3.3801e-01,  4.5440e-01, -8.0249e-01, -1.2952e+00, -7.5018e-01],\n",
      "         [-1.3120e+00, -2.1883e-01, -2.4351e+00, -7.2915e-02, -3.3986e-02]],\n",
      "\n",
      "        [[ 7.9689e-01, -1.8484e-01, -3.7015e-01, -1.2103e+00,  1.1404e+00],\n",
      "         [-8.9882e-02,  7.2980e-01, -1.8453e+00, -2.5020e-02,  1.3694e+00],\n",
      "         [ 2.6570e+00,  9.8512e-01,  3.7718e-01,  1.1012e+00, -1.1428e+00],\n",
      "         [ 3.7585e-02,  2.6963e+00,  1.2358e+00,  5.4283e-01,  5.2553e-01]]])\n"
     ]
    }
   ],
   "source": [
    "'''\n",
    "张量基本信息\n",
    "''' \n",
    "tensor = torch.randn(3,4,5)\n",
    "print(tensor.type())  # 数据类型\n",
    "print(tensor.size())  # 张量的shape，是个元组\n",
    "print(tensor.dim())   # 维度的数量\n",
    "print(tensor)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "64527ef9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[-6.2047e-01,  1.0594e+00,  1.0134e+00,  ...,  1.5947e+00,\n",
       "          -6.6700e-01, -1.4796e+00],\n",
       "         [ 1.0719e+00, -1.0995e+00,  7.3537e-01,  ...,  1.7845e+00,\n",
       "           6.4644e-01, -2.0119e-01],\n",
       "         [ 4.8083e-01,  6.4147e-01,  5.5666e-01,  ...,  1.2040e+00,\n",
       "          -9.8400e-01,  2.6019e-01],\n",
       "         ...,\n",
       "         [-5.4011e-01, -7.6446e-01, -8.6315e-01,  ..., -5.2706e-01,\n",
       "          -8.3338e-01, -9.7464e-02],\n",
       "         [ 5.2150e-02,  1.7890e+00,  3.9867e-01,  ..., -2.9800e-01,\n",
       "           1.3907e-01, -2.6343e-01],\n",
       "         [ 1.5219e+00,  4.9426e-01, -1.6572e+00,  ..., -3.2361e+00,\n",
       "          -1.0814e-01, -4.4812e-01]],\n",
       "\n",
       "        [[ 3.3043e-01,  3.4218e-01,  9.2788e-01,  ...,  8.1012e-01,\n",
       "           1.9266e-01,  1.7460e+00],\n",
       "         [-4.2765e-01,  6.8575e-01, -2.8343e-01,  ..., -8.4208e-01,\n",
       "          -8.7122e-01, -1.0261e+00],\n",
       "         [-1.4451e+00,  1.1112e+00, -2.3914e-01,  ..., -1.3927e+00,\n",
       "          -5.0399e-01, -2.0870e+00],\n",
       "         ...,\n",
       "         [ 5.9786e-01,  5.4486e-01,  8.4843e-01,  ..., -7.6500e-01,\n",
       "           1.2701e+00,  2.5468e+00],\n",
       "         [-2.3371e+00, -1.2025e+00,  4.1666e-02,  ..., -1.4901e+00,\n",
       "          -1.8612e-01,  1.9751e-01],\n",
       "         [ 2.0119e+00,  1.8887e+00,  3.3567e-01,  ...,  1.3667e+00,\n",
       "          -8.8326e-01, -4.8583e-01]],\n",
       "\n",
       "        [[ 1.6747e-01,  6.4569e-01, -1.0253e+00,  ..., -3.1714e-01,\n",
       "           1.5026e-01, -7.7616e-01],\n",
       "         [-5.2915e-01,  1.6139e-01, -1.9241e+00,  ..., -1.1460e+00,\n",
       "          -1.1248e-01,  6.7099e-01],\n",
       "         [ 6.3917e-01, -1.6896e+00,  4.0410e-01,  ...,  9.6380e-01,\n",
       "          -9.3361e-01,  8.1075e-01],\n",
       "         ...,\n",
       "         [ 2.2229e-01,  1.4176e+00, -3.6936e-01,  ...,  7.4659e-01,\n",
       "           8.4817e-01,  1.5913e-01],\n",
       "         [ 1.1880e+00, -4.3718e-01, -2.7484e-01,  ...,  2.2433e-01,\n",
       "           2.0388e+00, -1.3110e+00],\n",
       "         [-9.3067e-02, -1.5636e+00,  1.3372e+00,  ...,  4.6142e-01,\n",
       "           6.4305e-01,  2.2078e-01]],\n",
       "\n",
       "        ...,\n",
       "\n",
       "        [[-9.4715e-01,  1.2591e+00,  4.4644e-01,  ...,  9.7743e-01,\n",
       "           1.7477e-01,  1.0972e+00],\n",
       "         [-4.1129e-01, -6.2852e-01, -9.6042e-01,  ...,  5.7701e-02,\n",
       "           1.0205e+00, -8.3183e-01],\n",
       "         [ 2.8826e-01,  8.2595e-01, -4.7889e-01,  ...,  1.3756e+00,\n",
       "           2.0948e+00, -1.5475e-01],\n",
       "         ...,\n",
       "         [-9.1141e-01,  4.2287e-01,  2.4220e-01,  ..., -1.0746e+00,\n",
       "          -1.0078e+00,  2.7023e+00],\n",
       "         [ 3.6543e-02,  9.1783e-01,  2.5226e+00,  ..., -1.9101e-01,\n",
       "           8.8415e-01, -7.8943e-01],\n",
       "         [-7.5821e-01,  1.9084e+00, -2.3885e+00,  ...,  1.2945e+00,\n",
       "          -5.8487e-01, -1.7499e+00]],\n",
       "\n",
       "        [[ 1.1120e+00, -4.2210e-01, -3.4529e-01,  ...,  8.7195e-01,\n",
       "           7.7845e-01, -5.8048e-01],\n",
       "         [ 1.8182e+00,  9.1039e-01,  3.7783e-01,  ...,  7.9065e-01,\n",
       "           3.0495e-01,  1.3277e-01],\n",
       "         [-8.6731e-01,  6.5410e-01, -3.6041e-01,  ...,  2.6317e-03,\n",
       "          -3.7578e-01,  1.2374e+00],\n",
       "         ...,\n",
       "         [-6.5770e-01, -6.4835e-01, -3.0765e-01,  ..., -4.3663e-01,\n",
       "           2.4060e+00,  1.0308e+00],\n",
       "         [ 3.9642e-01,  1.5257e+00,  4.1516e-01,  ...,  1.3330e+00,\n",
       "          -1.1859e-01, -1.8340e+00],\n",
       "         [ 1.4062e+00, -2.3650e+00, -9.8923e-01,  ..., -9.8898e-01,\n",
       "          -6.9014e-01, -9.9999e-01]],\n",
       "\n",
       "        [[-6.3301e-01,  3.5478e-01,  5.8838e-01,  ..., -4.8438e-01,\n",
       "           2.0763e-01, -7.7387e-01],\n",
       "         [-1.0090e+00, -3.3628e-01, -7.8265e-01,  ..., -9.3572e-01,\n",
       "          -1.8007e+00, -1.7980e+00],\n",
       "         [ 4.1879e-01,  8.9509e-01,  9.3911e-01,  ...,  5.0305e-01,\n",
       "          -1.1534e+00,  1.3077e+00],\n",
       "         ...,\n",
       "         [ 8.9106e-01,  2.4879e+00, -6.2003e-01,  ..., -1.4339e-01,\n",
       "          -1.4572e+00,  9.8953e-01],\n",
       "         [ 1.3285e+00,  9.0997e-01,  2.8529e-01,  ...,  1.1045e+00,\n",
       "           8.1940e-01,  1.3137e-01],\n",
       "         [-3.3158e-01,  2.2196e-01, -1.8278e+00,  ...,  2.7826e-01,\n",
       "          -2.3074e+00,  1.1542e+00]]])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'''\n",
    "命名张量\n",
    "张量命名是一个非常有用的方法，这样可以方便地使用维度的名字\n",
    "来做索引或其他操作，大大提高了可读性、易用性，防止出错。\n",
    "''' \n",
    "# 在PyTorch 1.3之前，需要使用注释\n",
    "# Tensor[N, C, H, W]\n",
    "images = torch.randn(32, 3, 56, 56)\n",
    "images.sum(dim=1)\n",
    "images.select(dim=1, index=0)\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bbeff3b6",
   "metadata": {},
   "outputs": [],
   "source": [
    "# PyTorch 1.3之后\n",
    "NCHW = ['N', 'C', 'H', 'W']\n",
    "images = torch.randn(32, 3, 56, 56, names=NCHW)\n",
    "images.sum('C')\n",
    "images.select('C', index=0)\n",
    "# 也可以这么设置\n",
    "tensor = torch.rand(3,4,1,2,names=('C', 'N', 'H', 'W'))\n",
    "# 使用align_to可以对维度方便地排序\n",
    "tensor = tensor.align_to('N', 'C', 'H', 'W')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "b2bb2bb0",
   "metadata": {},
   "outputs": [],
   "source": [
    "'''\n",
    "数据类型转换\n",
    "'''\n",
    "# 设置默认类型，pytorch中的FloatTensor远远快于DoubleTensor\n",
    "torch.set_default_tensor_type(torch.FloatTensor)\n",
    "\n",
    "# 类型转换\n",
    "tensor = tensor.cuda()\n",
    "tensor = tensor.cpu()\n",
    "tensor = tensor.float()\n",
    "tensor = tensor.long()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6aa2367b",
   "metadata": {},
   "outputs": [],
   "source": [
    "'''\n",
    "torch.Tensor与np.ndarray转换\n",
    "除了CharTensor，其他所有CPU上的张量都支持转换为numpy格式然后再转换回来。\n",
    "'''\n",
    "ndarray = tensor.cpu().numpy()\n",
    "tensor = torch.from_numpy(ndarray).float()\n",
    "tensor = torch.from_numpy(ndarray.copy()).float() # If ndarray has negative stride.\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
